namespace Peer.Web.Controls.Upload
{
    using System;
    using System.Text;
    using System.Web;

    internal class WebbUploadHandler : IHttpHandler
    {
        public void InitProgress(string uploadGUID)
        {
            UploadStatus uploadStatus = new UploadStatus();
            uploadStatus.GetUploadStatus(uploadGUID);
            StringBuilder html = WebbHelper.GetHtml("Progress.page");
            if (!uploadStatus.IsActive)
            {
                html.Replace("${Script}$", "<script>window.opener=self;window.close();</script>");
            }
            else
            {
                switch (uploadStatus.Status)
                {
                    case UploadStatus.UploadState.Initializing:
                        html.Replace("${status}$", "Initializtion...");
                        break;

                    case UploadStatus.UploadState.Uploading:
                        html.Replace("${status}$", "Uploading...");
                        break;

                    case UploadStatus.UploadState.Uploaded:
                        html.Replace("${status}$", "Upload completed.");
                        break;

                    case UploadStatus.UploadState.Moving:
                        html.Replace("${status}$", "Moving file...");
                        break;

                    case UploadStatus.UploadState.Completed:
                        html.Replace("${status}$", "Finished.");
                        break;
                }
                this.ReviewStatus(uploadStatus, html, uploadGUID);
            }
            HttpContext context = WebbHelper.GetContext();
            context.Response.Expires = 0;
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentEncoding = Encoding.Default;
            context.Response.ContentType = "text/html";
            context.Response.Clear();
            context.Response.Write(html.ToString());
        }

        public void ProcessRequest(HttpContext context)
        {
            string uploadGUID = context.Request.QueryString["UploadID"];
            string filePath = context.Request.FilePath;
            filePath = filePath.Substring(filePath.LastIndexOf("/") + 1).ToUpper();
            bool flag = false;
            if (filePath == "PROGRESS.ASHX")
            {
                this.InitProgress(uploadGUID);
            }
            else
            {
                flag = true;
            }
            if (flag)
            {
                throw new HttpException(500, "unknown request");
            }
        }

        private void ReviewStatus(UploadStatus uploadStatus, StringBuilder builder, string uploadGUID)
        {
            if (uploadStatus.Status == UploadStatus.UploadState.Uploading)
            {
                builder.Replace("${FileName}$", uploadStatus.FileName);
                builder.Replace("${UploadProgress}$", uploadStatus.Percent.ToString());
                builder.Replace("${SurplusProgress}$", Convert.ToString((long) (100L - uploadStatus.Percent)));
                builder.Replace("${UploadSpeed}$", WebbHelper.GetFormatString((double) uploadStatus.Speed) + "/s");
                builder.Replace("${LeftTime}$", WebbHelper.GetFormatString(uploadStatus.LeftTime));
                builder.Replace("${BtnOK}$", "disabled");
                builder.Replace("${Refresh}$", "<meta http-equiv=\"Refresh\" content=\"1\";URL=progress.ashx?UploadID=" + uploadGUID + "\">");
            }
            else if (uploadStatus.Status == UploadStatus.UploadState.Completed)
            {
                builder.Replace("${UploadProgress}$", uploadStatus.Percent.ToString());
                builder.Replace("${SurplusProgress}$", Convert.ToString((long) (100L - uploadStatus.Percent)));
                builder.Replace("${FileName}$", uploadStatus.FileCount.ToString() + "file(s) uploaded success!");
                builder.Replace("${UploadSpeed}$", WebbHelper.GetFormatString((double) uploadStatus.Speed) + "/s");
                builder.Replace("${LeftTime}$", "finished, no time remain!");
                uploadStatus.Dispose();
                builder.Replace("${BtnOK}$", "onclick=\"javascript:window.opener=self;window.close();return false;\"");
                builder.Replace("${Refresh}$", "");
            }
            else
            {
                builder.Replace("${FileName}$", "Loading...");
                builder.Replace("${UploadProgress}$", "0");
                builder.Replace("${SurplusProgress}$", "100");
                builder.Replace("${UploadSpeed}$", WebbHelper.GetFormatString((double) uploadStatus.Speed) + "/s");
                builder.Replace("${LeftTime}$", "0 second(s)");
                builder.Replace("${BtnOK}$", "disabled");
                builder.Replace("${Refresh}$", "<meta http-equiv=\"Refresh\" content=\"1\";URL=progress.ashx?UploadID=" + uploadGUID + "\">");
            }
            if (uploadStatus.Status == UploadStatus.UploadState.Completed)
            {
                builder.Replace("${BtnCancel}$", "onclick=\"javascript:window.opener=self;window.close();return false;\"");
            }
            else if (WebbHelper.IsAccordantBrowser())
            {
                builder.Replace("${BtnCancel}$", "onclick=\"javascript:dialogArguments.location.href=dialogArguments.location.href;window.close();\"");
            }
            else
            {
                builder.Replace("${BtnCancel}$", "onclick=\"javascript:window.opener.opener=null;window.opener.location.href=window.opener.location.href;window.close();this.disabled=true;\"");
            }
            builder.Replace("${Script}$", "");
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

